﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="AuthorizeAttribute.cs" company="ecblog.codeplex.com">
//   emrecoskun.net - ibrahimatay.com
// </copyright>
// <summary>
//   The authorize attribute
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace EcBlog.Filters
{
    using System;
    using System.ComponentModel.Composition;
    using System.Web.Mvc;

    using EcBlog.Models;

    /// <summary>
    /// The authorize attribute
    /// </summary>
    [Export]
    public class AuthorizeAttribute : FilterAttribute, IAuthorizationFilter
    {
        private readonly PortalRole portalRole;

        /// <summary>
        /// Initializes a new instance of the <see cref="AuthorizeAttribute"/> class.
        /// </summary>
        /// <param name="portalRole">The portal role.</param>
        public AuthorizeAttribute(PortalRole portalRole)
        {
            this.portalRole = portalRole;
        }

        /// <summary>
        /// Called when authorization is required.
        /// </summary>
        /// <param name="filterContext">The filter context.</param>
        public void OnAuthorization(AuthorizationContext filterContext)
        {
            try
            {
                if (!filterContext.HttpContext.User.Identity.IsAuthenticated)
                {
                    if (filterContext.HttpContext.Request.Url != null)
                    {
                        var redirectOnSuccess = filterContext.HttpContext.Request.Url.AbsolutePath;
                        var logintUrl = "~/Account/Login" + "?ReturnUrl=" + redirectOnSuccess;
                        filterContext.HttpContext.Response.Redirect(logintUrl, true);
                    }
                }
                else
                {
                    if (filterContext.HttpContext.User.Identity.AuthenticationType != "ECnet")
                    {
                        // var openId = this._userRepository.GetOpenId(filterContext.HttpContext.User.Identity.Name);
                        // var id = new OpenIdIdentity(
                        //    filterContext.HttpContext.User.Identity.Name,
                        //    new UserInfo
                        //        {
                        //            Email = openId.User.Email,
                        //            FullName = openId.User.Email,
                        //            UserId = openId.UserId,
                        //            Role = Enum.GetName(typeof(PortalRole), (PortalRole)openId.User.PortalRole),
                        //            Username = openId.User.Username
                        //        });
                        // var principal = new GenericPrincipal(id, new[] { id.UserInfo.Role });
                        // filterContext.HttpContext.User = principal;
                    }

                    if (!filterContext.HttpContext.User.IsInRole(Enum.GetName(typeof(PortalRole), this.portalRole)))
                    {
                        throw new Exception("You are not authorized to view this page");
                    }
                }
            }
            catch (Exception exception)
            {
                Elmah.ErrorSignal.FromCurrentContext().Raise(exception);
                throw;
            }
        }
    }
}